using IQIGame.Onigao.Framework;
using IQIGame.Onigao.Game;

namespace IQIGame.Onigao.GamePlay
{
    public class UIPetWarehouse : UIBaseWindow
    {
        #region Template Generate,don't modify
        protected class UIB_UIPetWarehouse
        {
            #region ObjectBinding Generate
            public UnityEngine.GameObject topLeftBack { protected set; get; }
            public UnityEngine.GameObject moneyPanel { protected set; get; }
            public UnityEngine.GameObject baseInfo { protected set; get; }
            public UnityEngine.GameObject emptyInfo { protected set; get; }
            public UnityEngine.GameObject pointModel { protected set; get; }
            public UnityEngine.GameObject warehouse { protected set; get; }
            public IQIGame.Onigao.Framework.ExButton btnWarehouse { protected set; get; }
            public IQIGame.Onigao.Framework.ExButton btnShowModel { protected set; get; }
            public virtual void InitBinding(ObjectBinding __binding)
            {
                __binding.TryGetVariableValue<UnityEngine.GameObject>("topLeftBack", out var __tbv0);
                this.topLeftBack = __tbv0;
                __binding.TryGetVariableValue<UnityEngine.GameObject>("moneyPanel", out var __tbv1);
                this.moneyPanel = __tbv1;
                __binding.TryGetVariableValue<UnityEngine.GameObject>("baseInfo", out var __tbv2);
                this.baseInfo = __tbv2;
                __binding.TryGetVariableValue<UnityEngine.GameObject>("emptyInfo", out var __tbv3);
                this.emptyInfo = __tbv3;
                __binding.TryGetVariableValue<UnityEngine.GameObject>("pointModel", out var __tbv4);
                this.pointModel = __tbv4;
                __binding.TryGetVariableValue<UnityEngine.GameObject>("warehouse", out var __tbv5);
                this.warehouse = __tbv5;
                __binding.TryGetVariableValue<IQIGame.Onigao.Framework.ExButton>("btnWarehouse", out var __tbv6);
                this.btnWarehouse = __tbv6;
                __binding.TryGetVariableValue<IQIGame.Onigao.Framework.ExButton>("btnShowModel", out var __tbv7);
                this.btnShowModel = __tbv7;
            }
            #endregion ObjectBinding Generate 
        }
        #endregion Template Generate,don't modify

        #region Window Config
        /// <summary>
        /// 静态配置
        /// </summary>
        private static UICreateConfig _createConfig = new()
        {
            prefabName = nameof(UIPetWarehouse),
            layer = EUILayer.Normal,
            isFullScreen = true,
        };

        /// <summary>
        /// 创建UI的配置
        /// </summary>
        public override IUICreateConfig createConfig => _createConfig;
        #endregion Window Config

        #region fields
        private UIN_MoneyPanel topMoney = null;
        private UIN_PetBaseInfo petBaseInfo = null;
        private UIN_PetWarehouse petWarehouse = null;

        private bool isModelState = false;
        private Pet currPet = null;
        private UIEntityViewHelper entityHelper = null;

        private bool isShowWareHose = true;
        #endregion fields

        #region properties

        protected UIB_UIPetWarehouse ui { get; set; }
        protected UIMsgDispatcher uiMsgDispatcher => this._msgDispatcher as UIMsgDispatcher;

        #endregion properties

        protected override void BeforeInit()
        {
            ui = new UIB_UIPetWarehouse();
            ui.InitBinding(this.csObjBind);
        }

        protected override void OnInit()
        {
            this.topMoney = this.InitChildNode<UIN_MoneyPanel>(this.ui.moneyPanel);
            this.petBaseInfo = this.InitChildNode<UIN_PetBaseInfo>(this.ui.baseInfo);
            this.petWarehouse = this.InitChildNode<UIN_PetWarehouse>(this.ui.warehouse);

            this.topMoney.Show(nameof(UIPetWarehouse));
            var topLeftBack = this.InitChildNode<UIN_TopLeftBack>(this.ui.topLeftBack);
            topLeftBack.Show();
        }

        protected override void OnShow(UIBaseData data)
        {
            this.SetData(data);
            this.ShowView();
        }

        protected override void OnHide()
        {

        }

        /// <summary>
        /// 添加UGUI侦听
        /// </summary>
        protected override void AddUGUIListener()
        {
            this.ui.btnShowModel.onClick.AddListener(this.OnClickBtnShowMode);
            this.ui.btnWarehouse.onClick.AddListener(this.OnClickBtnShowWarehouse);
        }

        /// <summary>
        /// 添加全局侦听（MsgDispatcher）
        /// </summary>
        protected override void AddMsgListeners()
        {

        }

        protected override void OnDispose()
        {
            this.ClearModel();
        }

        #region methonds
        private void OnClickBtnShowMode()
        {
            this.isModelState = true;
            this.ShowPet(this.currPet);
            this.ShowBtns();
        }

        private void OnClickBtnShowWarehouse()
        {
            this.isModelState = false;
            this.ShowWarehouse();
            this.ShowBtns();
        }

        private void SetData(object data)
        {
            this.currPet = data as Pet;
            bool isHasPet = this.currPet != null;
            this.isShowWareHose = !isHasPet;
            this.isModelState = isHasPet;
        }

        private void ShowView()
        {
            if (this.isShowWareHose)
            {
                this.ShowWarehouse();
            }
            else
            {
                this.ShowPet(this.currPet);
            }
            this.ShowBtns();
        }

        private bool IsInModelState()
        {
            return this.isModelState && this.currPet != null;
        }

        private void ShowBtns()
        {
            bool isActive = this.isShowWareHose;
            bool isModelState = isActive && this.IsInModelState();
            this.ui.btnWarehouse.SetActive(isModelState);
            bool isActiveBtnModel = isActive && this.currPet != null && !isModelState;
            this.ui.btnShowModel.SetActive(isActiveBtnModel);
        }

        private void ShowWarehouse()
        {
            this.SetActiveModelParent(false);
            this.ShowWarehouseList();
        }

        private void ShowWarehouseList(long currPetId = -1)
        {
            this.petWarehouse.Show(this.OnWarehoseCall, currPetId);
        }

        private void OnWarehoseCall(Pet pet = null)
        {
            this.ShowPetDetail(pet);
        }

        private void ShowPet(Pet pet)
        {
            this.ShowPetDetail(pet);
            this.petWarehouse.Hide();
            this.SetActiveModelParent(true);
            this.ShowModel();
        }

        private void ShowPetDetail(Pet pet)
        {
            this.currPet = pet;
            this.petBaseInfo.Show(pet);
            this.ShowEmptyInfo(pet == null);
        }

        private void ShowEmptyInfo(bool isActive)
        {
            this.ui.emptyInfo.SetActive(isActive);
        }

        private void SetActiveModelParent(bool isActive)
        {
            this.ui.pointModel.SetActive(isActive);
        }

        private void ShowModel()
        {
            if (this.entityHelper == null)
                this.entityHelper = UIEntityViewHelper.NewFromPool();

            var cfgPet = this.currPet.cfgData;
            this.entityHelper.ShowModel(cfgPet.UIEntityAsset_fullPath, this.ui.pointModel);
        }

        private void ClearModel()
        {
            this.entityHelper?.Put2Pool();
            this.entityHelper = null;
        }

        public void RefreshCurrPetInfo(long petId)
        {
            var petData = GameDataCenter.PlayerData.pet;
            petData.TryGetValue(petId, out var currPet);
            this.currPet = currPet;

            bool isModelState = this.IsInModelState();
            if (isModelState)
            {
                this.ShowPetDetail(currPet);
            }
            else
            {
                petId = currPet?.id ?? -1;
                this.ShowWarehouseList(petId);
            }
            this.ShowBtns();
        }

        public void RefreshWarehouse()
        {
            this.isModelState = false;
            this.ShowView();
        }
        #endregion methonds
    }
}
